using System.Collections.Generic;
using UnityEngine;
using Verse;
using RimWorld;
using Verse.Sound;
using UnityEngine.AI;
using System;
using System.Linq;

namespace RimZombieGirl
{
    public static partial class PawnExts
    {
        public static bool HasEnoughSatiety(this Pawn pawn, int satiety)
        {
            float CurLevel = pawn?.needs?.food?.CurLevel ?? 0;
            return CurLevel >= satiety;
        }


        public static bool GeneExt_CanEatCorpse(this Pawn pawn)
        {
            PawnComp_GeneExt comp = pawn.GetComp<PawnComp_GeneExt>();
            if (comp != null && comp.allowEatCorpse)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 给Pawn添加ThingComp，顺便更新缓存
        /// </summary>
        /// <param name="pawn"></param>
        /// <param name="comp"></param>
        /// <returns></returns>
        public static bool AddComp(this Pawn pawn, ThingComp comp)
        {
            // 获取 comps 字段
            if (pawn.AllComps.Count == 0)
            {
                Log.Error("Pawn has no comps to add to.");
                return false;
            }

            pawn.AllComps.Add(comp);

            // 获取 compsByType 字典
            var compsByTypeField = typeof(Pawn).GetField("compsByType", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            var compsByType = compsByTypeField?.GetValue(pawn) as Dictionary<Type, ThingComp[]>;
            // 不必判空，一旦AllComps不为空，compsByType必然存在且非空
            var type = comp.GetType();
            if (compsByType.ContainsKey(type))
            {
                var arr = compsByType[type].ToList();
                arr.Add(comp);
                compsByType[type] = arr.ToArray();
            }
            else
            {
                compsByType[type] = new ThingComp[] { comp };
            }
            return true;
        }

        

    }
}